﻿<DemoPageSectionComponent Id="Editors-ComboBox-Cascading" ShowSizeMode="true">
    <ChildContentWithParameters Context="Params">
        @inject WorldcitiesDataService WorldcitiesDataService

        <div class="row cw-480">
            <div class="col-md-6">
                <label for="cbCountryName" class="demo-text mb-1">
                    Country Name
                </label>
                <DxComboBox Data="@Countries"
                            TextFieldName="@nameof(Country.CountryName)"
                            Value="@CurrentCountry"
                            ValueChanged="@((Country country) => SelectedCountryChanged(country))"
                            SizeMode="Params.SizeMode"
                            InputId="cbCountryName" />
            </div>
            <div class="col-md-6">
                <label for="cbCityName" class="demo-text mb-1">
                    City Name
                </label>
                <DxComboBox Data="@CurrentCountryCities"
                            TextFieldName="@nameof(City.CityName)"
                            @bind-Value="@CurrentCity"
                            SizeMode="Params.SizeMode"
                            InputId="cbCityName" />
            </div>
        </div>
    </ChildContentWithParameters>

    @code {
        IEnumerable<Country> Countries { get; set; }
        IEnumerable<City> Cities { get; set; }
        IEnumerable<City> CurrentCountryCities { get; set; }
        Country CurrentCountry { get; set; }
        City CurrentCity { get; set; }

        protected override async Task OnInitializedAsync() {
            Countries = await WorldcitiesDataService.GetCountriesAsync();
            Cities = await WorldcitiesDataService.GetCitiesAsync();
            CurrentCountry = Countries.FirstOrDefault(c => c.CountryName == "USA");
            CurrentCountryCities = Cities.Where(c => c.CountryId == CurrentCountry.CountryId);
            CurrentCity = CurrentCountryCities.FirstOrDefault();
        }

        void SelectedCountryChanged(Country country) {
            CurrentCountry = country;
            CurrentCountryCities = Cities.Where(c => c.CountryId == CurrentCountry.CountryId);
            CurrentCity = CurrentCountryCities.FirstOrDefault();
        }
    }
</DemoPageSectionComponent>
